package cn.stylefeng.guns.modular.courseversion.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.courseversion.entity.CourseVersion;
import cn.stylefeng.guns.modular.courseversion.mapper.CourseVersionMapper;
import cn.stylefeng.guns.modular.courseversion.model.params.CourseVersionParam;
import cn.stylefeng.guns.modular.courseversion.model.result.CourseVersionResult;
import  cn.stylefeng.guns.modular.courseversion.service.CourseVersionService;
import cn.stylefeng.guns.modular.stage.mapper.SpecialtyTypeMapper;
import cn.stylefeng.guns.modular.stage.model.result.SpecialtyTypeResult;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程版本 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-19
 */
@Service
public class CourseVersionServiceImpl extends ServiceImpl<CourseVersionMapper, CourseVersion> implements CourseVersionService {

    @Autowired
    private SpecialtyTypeMapper specialtyTypeMapper;

    @Override
    public void add(CourseVersionParam param){
        CourseVersion entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CourseVersionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CourseVersionParam param){
        CourseVersion oldEntity = getOldEntity(param);
        CourseVersion newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CourseVersionResult findBySpec(CourseVersionParam param){
        return null;
    }

    @Override
    public List<CourseVersionResult> findListBySpec(CourseVersionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CourseVersionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<Map<String, Object>> showCourseVersion() {
        List<SpecialtyTypeResult> list = specialtyTypeMapper.customList(null);
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        for (SpecialtyTypeResult specialtyTypeResult : list){
            Map<String, Object> map = new HashMap<>();
            map.put("specialtyName",specialtyTypeResult.getTypeName());
            map.put("versionIds",this.baseMapper.showCourseVersion(specialtyTypeResult.getId()));
            resultList.add(map);
        }
        return resultList;
    }

    private Serializable getKey(CourseVersionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CourseVersion getOldEntity(CourseVersionParam param) {
        return this.getById(getKey(param));
    }

    private CourseVersion getEntity(CourseVersionParam param) {
        CourseVersion entity = new CourseVersion();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
